package db

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/config"
	_ "github.com/go-sql-driver/mysql"
	"time"
	"xorm.io/xorm"
)

var (
	engine *xorm.Engine
)

// Init 初始化db
func Init(driver, source string, open, idle int) error {
	var err error
	engine, err = xorm.NewEngine(driver, source)
	if err != nil {
		return err
	}
	engine.ShowSQL(config.Log().LogInConsole)
	engine.SetMaxOpenConns(open)
	engine.SetMaxIdleConns(idle)
	return nil
}

// Session 获取连接
func Session() *xorm.Session {
	return engine.NewSession()
}

func TZLocation() *time.Location {
	return engine.TZLocation
}
